<template>
  <a-card>
    <div class="flex gap-3 items-center justify-between" v-if="item">
      <div v-if="type === '0' || type === '2'">
        <a-range-picker v-model="item.raw" show-time @change="setDate" @ok="setDate" />
      </div>

      <div v-if="type === '1' || type === '3'">
        <a-time-picker v-model="item.raw" type="time-range" @change="setDate" @ok="setDate" />
      </div>

      <a-button @click="del"> {{ t["删除"] }} </a-button>
    </div>
  </a-card>
</template>

<script setup lang="ts">
import { PublishTime } from "@/api/service/publicize/types";
import { ScheduleTypeEnum } from "@/enum/system.enum";

const t = translate("公共模块");

const emit = defineEmits(["del", "change"]);

const props = defineProps<{ value: PublishTime; type: ScheduleTypeEnum }>();
const item = defineModel<PublishTime>("item");

const del = () => {
  emit("del");
};
const setDate = () => {
  let start = "";
  let end = "";

  if (!item.value?.raw) return;
  /* 每天都执行|自定义日期 */
  if ([ScheduleTypeEnum.ONE_TIME, ScheduleTypeEnum.CUSTOM_DATE].includes(props.type)) {
    /* 执行一次 */
    const [startTime, endTime] = item.value.raw;
    const [startDate, sTime] = startTime.split(" ");
    const [endDate, eTime] = endTime.split(" ");
    start = sTime;
    end = eTime;
    item.value.work = [startDate, endDate];
  } else if (props.type === ScheduleTypeEnum.DAILY) {
    const [startTime, endTime] = item.value.raw;
    start = startTime;
    end = endTime;
    item.value.work = [startTime, endTime];
  } else if (props.type === ScheduleTypeEnum.WEEKLY) {
    const [startTime, endTime] = item.value.raw;
    start = startTime;
    end = endTime;
  }
  item.value.start = start;
  item.value.end = end;
  emit("change", item.value);
};
</script>

<style lang="scss" scoped></style>
